This script uses the construction method to dynamically create an
array of objects and the length
property to determine its size. The script user
doesn't have to worry about the number of elements or finding all the locations of specific
constants.
<script language="JavaScript"><!-- // ******************************************************** // To Use this Script, simply insert the Descriptions and // URLs below as indicated. // // That's all there is; no code to change. // // NOTE: use a \ before any ' or " characters that appear // in your descriptions // ******************************************************** // ********************************************* // Definition of our greeting object and methods // ********************************************* function greeting(start,end,greet) { this.start=start; this.end=end; this.greet=greet; } function greetingToString() { return this.greet; } greeting.prototype.toString = greetingToString; // ************************************** // Definition of an Array of link objects // ************************************** function greetingArray() { var a = greetingArray.arguments; if (a.length%3!=0) { // Make sure the number of arguments is a mulitple of three alert("Can not initialize Greeting Array: Incorrect number of strings"); this.length=0; } else { // Loop thru the argments creating link objects for (ti=ai=0;ai<a.length;ti++,ai+=3) { this[ti]=new greeting(a[ai],a[ai+1],a[ai+2]); } this.length=a.length/3; } } var greetingList = new greetingArray( // ***************************************************** // Place Times and Greetings Here, Separated by Commas // Format: start hour, end hour, Greeting // NOTE: use a \ before any ' or " characters that appear // in your Greetings // ***************************************************** 0,11,"morning", 12,17,"afternoon", 18,23,"evening" // ************************************ // !NOTE: No Comma after last Greeting! // ************************************ ) // *********************************** // Function to return correct greeting // *********************************** function TODgreeting() { var today = new Date(); var hr = today.getHours(); for(i=0;i<greetingList.length;i++) { if (hr>=greetingList[i].start && hr<=greetingList[i].end) { return greetingList[i]; } } return "Day" } <!-- **************************************************************** -- This is an example of embedding the greeting in the HTML Document -- ***************************************************************** --> You can also include the greeting in the body of the document this <script language="JavaScript"><!-- document.write(TODgreeting()); // --></script><noscript>day</noscript>. Just don't spend all <script language="JavaScript"><!-- document.write(TODgreeting()); // --></script><noscript>day</noscript> here.